﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Threading;

namespace Skyttespill
{
    public delegate void ColorEvent(Object sender, ColorEventArgs e);
    public delegate void LevelEvent(Object sender, LevelEventArgs e);

    public partial class MainForm : Form
    {
        private Color target_color;
        ArrayList high = new ArrayList();
        public event ColorEvent ColorChanged;
        public event LevelEvent LevelChanged;
        private float angle;
        ThreadStart keyPressedStart;
        Thread keyPressedLoop;


        public MainForm()
        {
            InitializeComponent();
            ColorChanged = new ColorEvent(this.SkyttePanel.ChangeColor);
            LevelChanged = new LevelEvent(this.SkyttePanel.ChangeLevel);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //System.Drawing.Graphics graphicsObj;
            //graphicsObj = this.CreateGraphics();
        }

        // Piltastene for å kunne bevege skytter, skyte og starte spill på nytt
        private void MoveSkytter(object sender, KeyEventArgs e)
        {
            //roterer kanonen
            if ((e.KeyCode == Keys.Right) || (e.KeyCode == Keys.Left))
            {
                if (e.KeyCode == Keys.Left)
                {
                    angle = -1.0f;             
                }
                if (e.KeyCode == Keys.Right)
                {
                    angle = 1.0f;
                }
                keyPressedStart = new ThreadStart(TurnCanon);
                keyPressedLoop = new Thread(keyPressedStart);
                keyPressedLoop.IsBackground = true;
                keyPressedLoop.Start();
            }

            //skyter med kuler
            if (e.KeyCode == Keys.Space) 
            {
                SkyttePanel.Skytt();
                SkyttePanel.Invalidate();
            }

            //starter spillet på nytt
            if (e.KeyCode == Keys.Enter)
            {
                this.SkyttePanel.StartGame();
            }
        }

        private void TurnCanon() 
        {
            try
            {
                while (true)
                {
                    this.SkyttePanel.Angle += angle;
                    Thread.Sleep(100); 
                }
            }
            catch (ThreadAbortException tae)
            {
                Console.WriteLine("Unntak:" + tae.Message);
            }
            finally
            {
            }
        }

        //Stopper tråd som snur kanonen
        private void StopMoveSkytter(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.Right))
            {
                keyPressedLoop.Abort();
                angle = 0.0f;
            }
        }

        // Oppdatering av Highscore listen
        private void UpdateHighScore(string _score)
        {
            string path = @"Highscore.txt";
            string score = _score;

            StreamReader readhigh = new StreamReader(path);

            string lines = string.Empty;

            while ((lines = readhigh.ReadLine()) != null)
            {
                high.Add(lines);
            }
            readhigh.Close();

            int count = high.Count;
            if (count > 9)
            {
                high.Add(score);
                high.Sort();
                high.Reverse();
                high.RemoveAt(10);
            }
            else
            {
                high.Add(score);
                high.Sort();
                high.Reverse();
            }

            StreamWriter writehigh = new StreamWriter(path, false);
            foreach (object s in high)
                writehigh.WriteLine(s.ToString());
            writehigh.Close();
        }

        // Menustrip > Help > Instructions
        private void instructionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("-Bruk høyre og venstre piltast for å kunne rotere skytter\n-Bruk tasten `SPACE` for å kunne avfyre skudd\n-Du skal skyte ned flest mulig mål men unngå å bli selv truffet\n-Blir du truffet av målene avsluttes spillet\n-Det går ikke å skyte ned murveggene på spillet\n-Fargene på målene du skal treffe bestemmer du selv i ´Settings´\n-Det finnes 3 vanskelighetsgrader: Easy,Medium eller Pro\nDu finner resultat ditt i Highscore listen under `Game`", "Instruksjon for Skytespill 2012");
        }

        // Menustrip > Help > About
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MenuAboutBox menuaboutbox = new MenuAboutBox();
            menuaboutbox.ShowDialog();
        }

        // Menustrip > Game > Start New Game
        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SkyttePanel.StartGame();
        }

        // Menustrip > Game > Finish Game
        private void finishGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SkyttePanel.FinishGame();
        }

        // Menustrip > Game > Highscore
        private void highscoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HighForm high = new HighForm();
            high.Show();
        }

        // Menustrip > Game > Exit
        private void finishToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // Menustrip > Settings > Difficulty > Easy
        private void easyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = true;
            mediumToolStripMenuItem.Checked = false;
            professionalToolStripMenuItem.Checked = false;

            LevelEventArgs lea = new LevelEventArgs(0);
            LevelChanged(this, lea);
        }

        // Menustrip > Settings > Difficulty > Medium
        private void mediumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = false;
            mediumToolStripMenuItem.Checked = true;
            professionalToolStripMenuItem.Checked = false;

            LevelEventArgs lea = new LevelEventArgs(1);
            LevelChanged(this, lea);
        }

        // Menustrip > Settings > Difficulty > Professional
        private void professionalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = false;
            mediumToolStripMenuItem.Checked = false;
            professionalToolStripMenuItem.Checked = true;

            LevelEventArgs lea = new LevelEventArgs(2);
            LevelChanged(this, lea);
        }

        // Menustrip > Settings > Target Color
        private void targetsColourToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult res = colDlg.ShowDialog();
            if (res == DialogResult.OK)
            {
                target_color = colDlg.Color;
                targetsColourToolStripMenuItem.BackColor = target_color;

                ColorEventArgs cea = new ColorEventArgs(target_color);
                ColorChanged(this, cea);
            }
        }
            
    }

    //
    public class LevelEventArgs : EventArgs
    {
        private int newLevel;

        public LevelEventArgs(int newLevel)
        {
            this.newLevel = newLevel;
        }
        public int NewLevel
        {
            get
            {
                return newLevel;
            }
            set
            {
                newLevel = value;
            }
        }
    }


    // Egendefinert event klasse
    public class ColorEventArgs : EventArgs
    {
        private Color farge;

        public ColorEventArgs(Color colour)
        {
            farge = colour;
        }
        public Color Farge
        {
            get
            {
                return farge;
            }
            set
            {
                farge = value;
            }
        }
    }
}
